package com.example.myprojectoffeixue;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class JuTixinghaoActivity extends Activity {

    private ListView lv;

    private ArrayList<HongWaiSB> parseArray;

    private MyApplication m;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ju_tixinghao);
    }

    @Override
    protected void onStart() {
        info();
        super.onStart();
    }

    private void info() {
        m = (MyApplication) getApplication();
        lv = (ListView) findViewById(R.id.lv_ju_tixinghao);
        Intent it = getIntent();
        String s1 = it.getStringExtra("id");
        HongWaiSB h = new HongWaiSB();
        h.companyId = s1;
        h.kind = m.type;

        String j = JSON.toJSONString(h);
        j = AESUtil.encrypt(j);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setHeader("Content-Type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(j, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        httpUtils.send(HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/remote/getModelList", params,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        HttpConnection.showToast(getBaseContext(),"网络异常");

                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        if(arg0.result != null && !("".equals(arg0.result))){
                        arg0.result = AESUtil.decrypt(arg0.result);
                        JSONObject j = JSONObject.parseObject(arg0.result);
                        String status=j.getString("status");
                        if("0".equals(status)){
                        JSONArray j1 = j.getJSONArray("modelList");
                        parseArray = (ArrayList<HongWaiSB>) JSONArray.parseArray(j1.toString(), HongWaiSB.class);
                            if(parseArray.size()!=0){
                                      HWadapter h = new HWadapter(parseArray, JuTixinghaoActivity.this, 3);
                                      lv.setAdapter(h);
                            }else{
                                HttpConnection.showToast(JuTixinghaoActivity.this,"该品牌下没有型号");
                            }


                        }else{

                            HttpConnection.showToast(JuTixinghaoActivity.this,"服务器错误");
                        }

                    }else{
                            HttpConnection.showToast(JuTixinghaoActivity.this,"服务器错误");
                    }}
                });

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (m.lsb6.size() != 0) {

                    if ("04".equals(m.type)) {
                        Intent it = new Intent(JuTixinghaoActivity.this, StudyYaoKongQiActivity.class);
                        it.putExtra("num", parseArray.get(position).num);
                        startActivity(it);

                    } else if ("02".equals(m.type)) {
                        Intent it = new Intent(JuTixinghaoActivity.this, StudyjdhActivity.class);
                        it.putExtra("num", parseArray.get(position).num);
                        startActivity(it);


                    } else if ("06".equals(m.type)) {

                        Intent it = new Intent(JuTixinghaoActivity.this, StudyKTActivity.class);
                        it.putExtra("num", parseArray.get(position).num);
                        startActivity(it);

                    }
                } else {

                    HttpConnection.showToast(getBaseContext(),"未添加红外学习转发器");
                }

            }

        });

    }

}
